//
//  LFAddressViewController.m
//  LoveFruit
//
//  Created by Jack on 16/9/24.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFAddressController.h"
#import "LFAddressTableController.h"
#import "LFAddressAddController.h"
#import "LFAddressAddTableController.h"

@interface LFAddressController ()

@end

@implementation LFAddressController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置导航栏
    [self setupNav];

    // 加载tableView
    [self loadTableView];
    
    // 加载底部白色视图
    [self loadBottomView];
 
}

// 设置导航栏
- (void)setupNav {
    // 导航条导致的下移
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 导航栏标题
    self.navigationItem.title = @"我的收货地址";
    // 导航栏返回按钮
    UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_backItem"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(dismissClick)];
    self.navigationItem.leftBarButtonItem = returnButtonItem;
}

- (void)dismissClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 加载tableView
- (void)loadTableView {
    LFAddressTableController *addressTableVC = [[LFAddressTableController alloc] initWithStyle:UITableViewStyleGrouped];
    [self.view addSubview:addressTableVC.tableView];
    // 控制器建立父子关系
    [self addChildViewController:addressTableVC];
    [addressTableVC didMoveToParentViewController:self];
    
    [addressTableVC.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.top.offset(64);
    }];
}


// 加载底部视图
- (void)loadBottomView {
    // create底部白色视图
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.right.equalTo(self.view);
    }];
    //底部视图上的Btn
    UIButton *addAddressBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   
    [bottomView addSubview:addAddressBtn];
    
    addAddressBtn.backgroundColor = [UIColor colorWithHex:0xfcc809];
    
    [addAddressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(50);
        make.right.offset(-50);
        make.top.offset(12);
        make.bottom.offset(-12);
    }];
    // 按钮的标题
    [addAddressBtn setTitle:@"+ 新增地址" forState:UIControlStateNormal];
    addAddressBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    [addAddressBtn addTarget:self action:@selector(addAddressBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
// 跳转到'添加新地址'界面
- (void)addAddressBtnClick {
    // toVC
    LFAddressAddController *aAddVC = [[LFAddressAddController alloc] init];
    
    // 反向传值block赋值
    [aAddVC setAddNewAddress:^(LFAddressTableCellModel *newModel) {
        // 获取子控制器
        LFAddressTableController *adrTableVC = [self.childViewControllers firstObject];
        [adrTableVC.dataSourceArrM addObject:newModel];
        // 刷新列表
        [adrTableVC.tableView reloadData];
    }];
    
    [self.navigationController pushViewController:aAddVC animated:YES];
}
@end
